package com.sheep.jucdemo.part10.part10_1;

import java.util.concurrent.CountDownLatch;

/**
 * @Date 2022/4/27 22:09
 * @Created by sheep
 */
public class Test10_1_1 {
    // 创建一个countDownLatch实例
    private static volatile CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("child threadOne over");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("child threadTwo over");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
        // 启动子线程
        threadOne.start();
        threadTwo.start();

        System.out.println("wait all child thread over");
        // 等待子线程执行完毕，返回
        countDownLatch.await();

        System.out.println("all child thread over");
    }
}
